<template>
  <div id="login">
    <div class="name">
      昌融建材全产业链
      <br>工作台
    </div>
    <!-- 登入框 -->
    <div class="login-box">
      <div id="normalLoginTab" class="loginForm" style="display: block">
        <h2 class="loginbox-title">帐号登录</h2>
        <div class="loginUrs" style="height: 302px">
          <div class="g-bd">
            <!-- 登入表单 -->
            <el-form
              ref="Loginform"
              :model="user"
              style="width: 70%; margin: auto"
            >
              <el-form-item>
                <el-input v-model="user.account" placeholder="请输入账号" style="height: 40px; line-height: 40px;" />
              </el-form-item>
              <el-form-item>
                <el-input
                  v-model="user.password"
                  placeholder="请输入密码"
                  show-password
                />
              </el-form-item>
              <el-form-item style="margin-top: -20px">
                <el-checkbox v-model="checked" type="check"
                  >记住密码</el-checkbox
                >
                <router-link
                  to="/"
                  style="
                    margin-left: 175px;
                    text-decoration: none;
                    color: #42a5eb;
                  "
                  >忘记密码？</router-link
                >
              </el-form-item>
              <el-form-item style="margin-top: -30px">
                <el-button type="goon" @click="doLogin()"
                  >登&nbsp;&nbsp;入</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
    <el-row style="margin-top:300px;">
      <el-col :span="2" :offset="6">
        <el-link :underline="false">关于我们</el-link>
      </el-col>
     <el-col :span="2" :offset="1">
        <el-link :underline="false">服务条款</el-link>
      </el-col>
      <el-col :span="2" :offset="1">
        <el-link :underline="false">联系我们</el-link>
      </el-col>
      <el-col :span="2" :offset="1">
        <el-link :underline="false">客服中心</el-link>
      </el-col>
      <el-col :span="2" :offset="1">
        <el-link :underline="false">帮助中心</el-link>
      </el-col>
    </el-row>
    <div class="footer">@copy2021</div>
  </div>
</template>

<script>
import qs from "qs";
import axios from "axios";
export default {
  data() {
    return {
      user: {
        account: "",
        password: "",
        portrait:"",
        realname:"",
        phone:""
      },
      checked: [],
      errorInfo : false,
      errInfo:"",
      rules: {
        account: [
            { required: true, message: 'please enter username', trigger: 'blur' }
        ],
        password: [
            { required: true, message: 'please enter password', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    doLogin() {
      // debounceAjax(formName)
      const self = this;
      if (!self.user.account){
          alert('Please enter an account!')
      }
      else if(!self.user.password){
          alert('Please enter a password!')
      }
      else if(self.user.password&&self.user.account){      
          var dataa={account:this.user.account,password:this.user.password}   
          axios.post('/api/user/login',JSON.stringify(dataa))
          .then((response) => {
              if (response.data == -1) {
                  self.errorInfo = true;
                  self.errInfo = 'no such user';
                  console.log('Invalid username or password.')
              } else if (response.data == 0) {
                  console.log('Invalid username or password.')
                  self.errorInfo = true;
                  self.errInfo = 'Invalid username or password.';
              } else if (response.status == 200) {
                  var datab=response.data
                  sessionStorage.setItem('ms_account',self.user.account);
                  sessionStorage.setItem('ms_password',self.user.password);
                  sessionStorage.setItem('ms_user',JSON.stringify(datab));
                  alert(JSON.stringify(datab))
                  //self.$router.push('/index');//页面跳转
                  window.location.href='/index'
              }                            
          }).then((error) => {
              console.log(error);
          })
      } else {
          console.log('error submit!!');
          return false;
      }
    }
},
};
</script>

<style scoped src="../../style/Login.css">
</style>